package com.gopay.common.dts.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.dts.DtsBusinessActivityLog;
import com.gopay.common.dts.dao.DtsBusinessActivityLogDAO;

@Repository(value = "dtsBusinessActivityLogDAO")
public class DtsBusinessActivityLogDAOImpl extends BaseDAOImpl<DtsBusinessActivityLog, String> implements DtsBusinessActivityLogDAO {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public DtsBusinessActivityLog getByComp(String domain, String activity,
			String snapkey) {
		
		StringBuilder hql = new StringBuilder(" from DtsBusinessActivityLog log where 1=1");
		Map param = new HashMap();
		
		hql.append(" and log.domain = :Domain");
		param.put("Domain", domain);
		
		hql.append(" and log.activity = :Activity");
		param.put("Activity", activity);
		
		hql.append(" and log.snapkey = :SnapKey");
		param.put("SnapKey", snapkey);
		
		List<DtsBusinessActivityLog> logs = super.findByHql(hql.toString(), param);
		if(logs != null && !logs.isEmpty()) {
			if(logs.size() == 1) {
				return logs.get(0);
			} 
			else {
				throw new IllegalStateException("More than 1 Record Founded where data should be UNIQUE!");
			}
		}
		return null;
	}

}
